package test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import domain.Article;
import domain.Comment;
import domain.Orders;
import domain.User;
import mapper.ArticleMapper;
import mapper.CommentMapper;
import mapper.OrderMapper;
import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author cyt
 * @date 2020/9/5
 */
public class test {

    @Test
    public void test() throws IOException {

        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        PageHelper.startPage(2,2);
        List<User> all = userMapper.findAll();
        all.stream().forEach(user1 -> System.out.println(user1));

        PageInfo<User> userPageInfo = new PageInfo<User>(all);
        System.out.println(userPageInfo.getTotal());
        System.out.println(userPageInfo.getPageSize());
        System.out.println(userPageInfo.getPages());
        System.out.println(userPageInfo.getSize());


    }

    @Test
    public void insertTest() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();
        user.setUsername("c++");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("重庆");
        mapper.saveUser(user);
        System.out.println(user);
//        sqlSession.insert("UserMapper.saveUser",user);

        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void updateUserTest() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        User user = new User();
        user.setId(3);
        user.setUsername("python");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("重庆");

        sqlSession.update("domain.User.updateUser",user);

        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void deleteTest() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();


        sqlSession.delete("domain.User.deleteUser",3);

        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void test1() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(2);
        List<User> list = mapper.findByIdAndUsername(user);
        list.stream().forEach(user1 -> System.out.println(user));


        List<User> list1 = mapper.findByIdAndUsername(user);
        list1.stream().forEach(user1 -> System.out.println(user));



        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void test2() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.findAllWithOrder();
        users.stream().forEach(
                all -> System.out.println(all + "")
        );

        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test0_1() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);
        List<Article> all = mapper.findAll();
        all.stream().forEach(
                article -> {
                    System.out.println(article + "");
                    System.out.println(article.getComments());
                }
        );


        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test0_2() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sessionFactory.openSession();
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        List<Comment> all = mapper.findAll();
        all.stream().forEach(
                comment -> System.out.println(comment)
        );
    }
}
